#!/usr/bin/env python
# yongming.li 

import os
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import string

from DnDListWidget import *

import threading

class MonkeyTest(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.cmds = []
    def run(self):
        if len(self.cmds) == 0:
           retrun -1
        print 'begin run (threading)'
        for item in self.cmds:
            print item
            os.popen(item)
    def setcmds(self,value):
        self.cmds= value

class MonkeyTestForm(QDialog):

    def getallprocess(self):
        # adb shell ps
        self.packages=[]
        lines = os.popen('adb shell pm -l').readlines()
        for  line  in lines: 
            s=line.strip()
            item=s.split(':')

            self.packages.append(item[1])
        print self.packages
    def quit(self):
        self.close()
        print 'quit'

    def ok(self):
        print '---------------------------------------------------'
        count = self.dndIconListWidget.count()
        self.cmdlist = []
        for index in range(0,count):
            name = self.dndIconListWidget.item(index).text()
            #print 'begin test '+name
            #print os.popen('adb shell monkey -p  %s  5000'%(name)).readlines()
            cmd = 'adb shell monkey -p  %s  %s'%(name,self.monkeytestcount)
            self.cmdlist.append(cmd)
        self.monkeyTest.setcmds(self.cmdlist)
        self.monkeyTest.start()
             
    def setCountValue(self,value):
        self.monkeytestcount = value
        print 'setCountValue'
           
    def __init__(self, parent=None):
        super(MonkeyTestForm, self).__init__(parent)
        self.monkeytestcount = 1000
        self.monkeyTest = MonkeyTest()
        dndListWidget = DnDListWidget()
        path = os.path.dirname(__file__)
        self.getallprocess()
        for index in range(0,len(self.packages)):
            item = QListWidgetItem(self.packages[index])
            item.setIcon(QIcon(os.path.join('', './images/monkey/%d.jpg'%(index%10))))
            item.setIcon(QIcon(os.path.join('', './images/monkey/1.jpg')))
            dndListWidget.addItem(item)
 

        self.dndIconListWidget = DnDListWidget()
        self.dndIconListWidget.setMinimumSize(200,500)
        self.dndIconListWidget.setViewMode(QListWidget.IconMode)

        self.quitbutton = QPushButton()
        self.okbutton = QPushButton()
        self.addallbutton = QPushButton()
        self.removeallbutton = QPushButton()
        self.addallbutton.setText ('add all packages')
        self.removeallbutton.setText ('remove all packages')

        self.quitbutton.setText ('Quit monkey test')
        self.okbutton.setText ('Begin monkey test')
        self.connect(self.okbutton, SIGNAL('clicked()'), self.ok)
        self.connect(self.quitbutton, SIGNAL('clicked()'), self.quit)


        self.horizontalSliders = QSlider(Qt.Horizontal)
        self.valueSpinBox = QSpinBox()

        self.countvaluelabel = QLabel("test counts is :")
        self.horizontalSliders.setRange(1000, 100000)
        self.horizontalSliders.setSingleStep(1000)
        self.valueSpinBox.setRange(1000, 100000)
        self.valueSpinBox.setSingleStep(1000)

        self.valueSpinBox.valueChanged.connect(self.horizontalSliders.setValue)
        self.horizontalSliders.valueChanged.connect(self.valueSpinBox.setValue)
        self.valueSpinBox.valueChanged.connect(self.setCountValue)
        self.horizontalSliders.valueChanged.connect(self.setCountValue)
       
        

        
        #addlayout = QVBoxLayout()
        valuelayout = QHBoxLayout()
        addlayout = QGridLayout()
        addlayout.addWidget(self.addallbutton,0,0)
        addlayout.addWidget(self.removeallbutton,1,0)
        valuelayout.addWidget(self.countvaluelabel)
        valuelayout.addWidget(self.valueSpinBox)
        addlayout.addLayout(valuelayout,2,0)
        #addlayout.addWidget(self.countvaluelabel,2,0)
        addlayout.addWidget(self.horizontalSliders,3,0)
        #addlayout.addWidget(self.valueSpinBox,2,1)

     

        layout = QGridLayout()
        layout.addLayout(addlayout, 0, 1)
        layout.addWidget(dndListWidget, 0, 0)
        layout.addWidget(self.dndIconListWidget, 0, 2)




        layout.addWidget(self.okbutton, 1, 0)
        layout.addWidget(self.quitbutton, 1, 2)
        

        self.setLayout(layout)

        self.setWindowTitle("Monkey test")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = MonkeyTestForm()
    form.show()
    app.exec_()

